#include "SelectScene.h"

bool SelectScene::init() {
	if (!Scene::init()) return false;
	if (USERDEFAULT->getBoolForKey(MUSIC_ENABLE_KEY)) {
		AUDIOENGINE->playBackgroundMusic(MENUSCENE_BGM_NAME, true);
	}
	m_rootNode = CSLoader::createNode(SELECTSCENE_CSB_NAME);
	this->addChild(m_rootNode);

	m_gold = m_rootNode->getChildByName<Text *>(GOLDTEXT);
	m_jewel = m_rootNode->getChildByName<Text *>(JEWELTEXT);
	this->updateLabel();
	m_gold->setFontName(TEXTFONT_NAME);
	m_jewel->setFontName(TEXTFONT_NAME);

	m_bowLayer = m_rootNode->getChildByName(BowSELECTLAYER);
	m_levelLayer = m_rootNode->getChildByName(LEVELSELECTLAYER); 
	this->initBowIcon();
	this->initLevelLayer();

	m_levelLayer->setPosition(Vec2(0, 800));

	auto selectLevelButton = m_rootNode->getChildByName<Button *>(LEVELSELECTBUTTON);
	selectLevelButton->addClickEventListener([=](Ref *ref) {
		m_levelLayer->setVisible(true);
		m_levelLayer->onEnter();
		m_bowLayer->onExit();
		m_levelLayer->setPosition(Vec2(0, 0));
	});

	return true;
}

void SelectScene::initBowIcon() {
	std::string list[11] = BOW_LIST;
	__Array *preSkills[11] = PRESKILLS_LIST;
	Vec2 pos[11] = ICON_POSLIST;
	for (int i = 0; i < 10; i++) {
		auto button = SelectButton::createButton(list[i].c_str(), pos[i], this);
		button->setGlobalZOrder(10);
		button->setName(list[i].c_str());
		button->setPreSkills(preSkills[i]);
		m_bowLayer->addChild(button);
	}
	auto demonPreSkills = __Array::create();
	demonPreSkills->addObject(__String::createWithFormat(POWER_3_BOW));
	demonPreSkills->addObject(__String::createWithFormat(MULTPLE_3_BOW));
	demonPreSkills->addObject(__String::createWithFormat(SPEED_3_BOW));
	auto button = SelectButton::createButton(list[10].c_str(), pos[10], this);
	button->setName(list[10].c_str());
	button->setPreSkills(demonPreSkills);
	m_bowLayer->addChild(button);

	auto complete = m_rootNode->getChildByName<Button *>(COMPLETE);
	complete->addClickEventListener([=](Ref *ref) {
		if (USERDEFAULT->getBoolForKey(EFFECT_ENABLE_KEY))
			AUDIOENGINE->playEffect(CLICKEFFECT_NAME);
		DIRECTOR->replaceScene(GameScene::createScene(Player::getInstance()->getLevel()));
	});

	auto close = m_rootNode->getChildByName<Button *>(CLOSE);
	close->addClickEventListener([=](Ref *ref) {
		if (USERDEFAULT->getBoolForKey(EFFECT_ENABLE_KEY))
			AUDIOENGINE->playEffect(CLICKEFFECT_NAME);
		DIRECTOR->replaceScene(MenuScene::createScene());
	});
}

void SelectScene::initLevelLayer() {
	auto bowSelectButton = m_levelLayer->getChildByName<Button *>(BOWSELECTBUTTON);
	bowSelectButton->addClickEventListener([=](Ref *ref) {
		m_levelLayer->setVisible(false);
		m_levelLayer->onExit();
		m_bowLayer->onEnter();
		m_levelLayer->setPosition(Vec2(0, 800));
	});

	int unLockLevel = Player::getInstance()->getLevel();
	for (int i = 0; i < 12; i++) {
		auto button = LevelButton::createButton(i + 1);
		button->setPosition(120 * (i % 6) + 100, 320 - 120 * (i / 6));
		m_levelLayer->addChild(button);
		if (i < unLockLevel) {
			button->unLock();
		}
	}
}

void SelectScene::unselectButtons() {
	this->updateLabel();
	std::string list[11] = BOW_LIST;
	for (int i = 0; i < 11; i++) {
		auto button = m_bowLayer->getChildByName<SelectButton *>(list[i].c_str());
		if (button->getSelected()) {
			button->setSelected(false);
		}
	}
}

void SelectScene::updateLabel() {
	m_gold->setString(__String::createWithFormat("%d", USERDEFAULT->getIntegerForKey(GOLD_KEY))->getCString());
	m_jewel->setString(__String::createWithFormat("%d", USERDEFAULT->getIntegerForKey(JEWEL_KEY))->getCString());
}